<?php

declare(strict_types=1);

namespace App\Controller\store;

use App\Middleware\User\StoreMiddleware;
use App\Request\Store\Advertising\AddRequest;
use App\Service\Store\AdvertisingService;
use App\Service\Store\StoreService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 后台广告管理APP首页
 * Class AdvertisingController
 * @package App\Controller\store
 * @AutoController()
 * @Middleware(StoreMiddleware::class)
 * @author 陈奕文
 */
class AdvertisingController extends Controller
{
    /**
     * 后台服务
     * @Inject()
     * @var StoreService
     */
    private $store_service;

    /**
     * 广告服务
     * @Inject()
     * @var AdvertisingService
     */
    private $advertising_service;

    /**
     *  后台广告查询
     *  $type = 1
     */
    public function indexSearch()
    {
        $type = (int)$this->request->input('type', 1);
        $location_id = (int)$this->request->input('location_id', 0);
        $params = $this->request->all();
        $sort = 'created_at desc';
        $ads_list = $this->advertising_service->getIndexAdsList($type, $location_id,$params, $sort);
        $this->success($ads_list);
    }

    /**
     * 获取所有行业
     */
    public function industries()
    {
       $industries = $this->advertising_service->industry_dao->getBusinessIndustryInfo();
       $this->success($industries);
    }

    /**
     * 获取所有下级地区位置
     */
    public function locations()
    {
        $pid = (int)$this->request->input('pid', 0);
        $locations = $this->advertising_service->location_dao->getLocationsByMap([
            'level' => $pid
        ]);
        $this->success($locations);
    }

    /**
     *  广告 禁用 启用
     */
    public function status()
    {
        $id = (int)$this->request->input('id');
        $result = $this->advertising_service->statusChange($id);
        if ($result['is_success']) {
            $this->success(['status' => $result['status']]);
        } else {
            $this->error('ADVERTISING.STATUS_CHANGE_FAIL');
        }

    }

    /**
     * 广告删除
     */
    public function delete()
    {
        $id = (int)$this->request->input('id');
        $result = $this->advertising_service->dao->delete($id);
        if ($result) {
            $this->success($result);
        } else {
            $this->error('ADVERTISING.DELETE_FAIL');
        }
    }

    /**
     * 获取单个广告
     */
    public function single()
    {
        $id = (int)$this->request->input('id');
        $result = $this->advertising_service->dao
            ->getAllInfoById($id);
        if ($result) {
            $result = $result->makeHidden(['deleted_at','sort']);
        }
        $this->success($result);
    }

    /**
     * 广告添加
     * @param AddRequest $request
     */
    public function add(AddRequest $request)
    {
        $params = $request->all();

        $data = [];
        isset($params['type']) && $data['type'] = (int)$params['type']; // 广告类型
        isset($params['title']) && $data['title'] = $params['title']; // 广告标题
        isset($params['jump_type']) && $data['jump_type'] = (int)$params['jump_type']; // 跳转类型
        isset($params['jump_id']) && $data['jump_id'] = $params['jump_id']; // 跳转ID

        isset($params['link']) && $data['link'] = $params['link']; // 跳转链接
        !isset($params['link']) && $data['link'] = "";

//        (!isset($params['start_time']) || !isset($params['end_time'])) && $this->error("广告必须有开始时间和结束时间");
        $start_time = strtotime($params['start_time']);
        $end_time = strtotime($params['end_time']);
        if (!$start_time || !$end_time)
            $this->error('ADVERTISING.ERROR_TIME_FORMAT');
        $end_time <= $start_time && $this->error('ADVERTISING.END_LESS_START');

        isset($params['start_time']) && $data['start_time'] = $start_time; // 广告有效开始时间
        isset($params['end_time']) && $data['end_time'] = $end_time; // 广告有效结束时间

        isset($params['business_name']) && $data['business_name'] = $params['business_name']; // 店铺名称/公司名称
        isset($params['contacts']) && $data['contacts'] = $params['contacts']; // 联系人
        isset($params['phone']) && $data['phone'] = $params['phone']; // 联系方式
        isset($params['industry_id']) && $data['industry_id'] = (int)$params['industry_id']; //行业类别
        isset($params['position']) && $data['position'] = (int)$params['position']; // 广告所在屏幕位置ID

        isset($params['location_id']) && $data['location_id'] = (int)$params['location_id']; // 投放区域ID(当省级ID存在时)
        isset($params['city_id']) && $data['location_id'] = (int)$params['city_id']; // 投放区域ID(当市一级ID存在时)
        isset($params['district_id']) && $data['location_id'] = (int)$params['district_id']; // 投放区域ID(当区一级ID存在时)

        isset($params['status']) && $data['status'] = (int)$params['status']; // 广告状态ID
        !isset($params['status']) && $data['status'] = 0; // 默认禁用状态
        isset($params['image']) && $data['image'] = $params['image']; // 图片
        $data['sort'] = 0;
        // 广告添加
        $result = $this->advertising_service->dao->create($data);

        $result && $this->success($result);

        $this->error("ADVERTISING.ADD_FAIL");
    }

    /**
     * 广告修改
     */
    public function polish()
    {
        $id = (int)$this->request->input('id');
        !$id && $this->error();

        $advertising = $this->advertising_service->dao->getAllInfoById($id);
        !$advertising && $this->error("广告不存在");

        $params = $this->request->all();
        isset($params['type']) && $advertising->type = (int)$params['type']; // 广告类型
        isset($params['title']) && $advertising->title = $params['title']; // 广告标题
        isset($params['jump_type']) && $advertising->jump_type = (int)$params['jump_type']; // 跳转类型
        isset($params['jump_id']) && $advertising->jump_id = $params['jump_id']; // 跳转ID

        isset($params['link']) && $advertising->link = $params['link']; // 跳转链接
        // 开始 结束
        $start_time = $advertising->start_time;
        $end_time = $advertising->end_time;

        isset($params['start_time']) &&  $start_time = strtotime($params['start_time']) && !$start_time && $this->error('ADVERTISING.ERROR_TIME_FORMAT');
        isset($params['end_time']) && $end_time = strtotime($params['end_time']) && !$end_time && $this->error('ADVERTISING.ERROR_TIME_FORMAT');

        $end_time <= $start_time && $start_time  &&  $this->error('ADVERTISING.END_LESS_START');

        isset($params['start_time']) && $advertising->start_time = $start_time; // 广告有效开始时间
        isset($params['end_time']) && $advertising->end_time = $end_time; // 广告有效结束时间

        isset($params['business_name']) && $advertising->business_name = $params['business_name']; // 店铺名称/公司名称
        isset($params['contacts']) && $advertising->contacts = $params['contacts']; // 联系人
        isset($params['phone']) && $advertising->phone = $params['phone']; // 联系方式
        isset($params['industry_id']) && $advertising->industry_id = (int)$params['industry_id']; //行业类别
        isset($params['position']) && $advertising->position = (int)$params['position']; // 广告所在屏幕位置ID

        isset($params['location_id']) && $advertising->location_id = (int)$params['location_id']; // 投放区域ID(当省一级存在时)
        isset($params['city_id']) && $advertising->location_id = (int)$params['city_id']; // 投放区域ID（当市一级存在时）
        isset($params['district_id']) && $advertising->location_id = (int)$params['district_id']; // 投放区域ID（当区一级存在时）

        isset($params['status']) && $advertising->status = (int)$params['status']; // 广告状态ID
        isset($params['image']) && $advertising->image = $params['image']; // 图片
        isset($params['sort']) && $advertising->sort = (int)$params['sort']; // 排序

        $result = $advertising->save();
        !$result && $this->error("修改失败");

        $this->success($result);
    }
    

}
